package com.module.app.pop

import android.app.Activity
import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.module.app.adapter.holder.PayHolder
import com.module.base.base.adapter.BaseNewAdapter
import com.module.base.utils.RecycleViewUtil
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.module.app.R
import com.module.base.base.adapter.BaseAdapter
import com.module.frame.ext.postEvent
import com.module.pay.PayListener
import com.module.pay.ali.AliPayUtils
import com.module.pay.wx.WXPayUtils
import com.module.pay.wx.WxPayListener
import com.module.pay.wx.bean.WxPlayResult
import com.module.third.TemporaryData
import com.module.third.bean.IThirdType
import com.module.third.bean.IThirdType.IBuyType
import com.module.third.bus.PayBus
import com.module.third.ext.payLiveSharedFlow
import com.module.third.wx.WxUtils
import java.math.BigDecimal

class PayPop(
    context: Context, @IBuyType buyType: Int,
    payBean: com.module.app.bean.PayBean,
    price: BigDecimal,
    text: String,
    extraList: List<*>
) : BottomPopupView(context) {

    companion object {
        fun show(
            context: Context?,
            @IBuyType buyType: Int,
            payBean: com.module.app.bean.PayBean,
            price: BigDecimal,
            text: String,
            vararg extra: Any?
        ) {
            if (context == null) {
                return
            }
            val payPop = XPopup.Builder(context)
                .hasShadowBg(true)
                .isDestroyOnDismiss(true)
                .asCustom(PayPop(context, buyType, payBean, price, text, extra.toList())) as PayPop
            payPop.show()
        }

    }

    lateinit var recyclerview: RecyclerView

    @IBuyType
    private var buyType = buyType
    private var payBean: com.module.app.bean.PayBean = payBean
    private var price: BigDecimal = price
    private var text: String = text
    private val extraList = extraList

    private var baseNewAdapter: BaseAdapter = object : BaseAdapter() {
        override fun onCreateDefaultViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): PayHolder {
            return PayHolder(parent)
        }
    }

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_pay
    }

    override fun onCreate() {
        super.onCreate()
        recyclerview = findViewById(R.id.recyclerview)
        recyclerview.layoutManager = RecycleViewUtil.getLinear(context)
        baseNewAdapter.bindToRecyclerView(recyclerview)
        setData(buyType, payBean, price, text, extraList)
    }

    private fun setData(
        @IBuyType buyType: Int,
        payBean: com.module.app.bean.PayBean,
        price: BigDecimal,
        text: String?,
        extraList: List<*>
    ) {
        TemporaryData.put(IThirdType.ITemporaryCache.BUY_TYPE, buyType)
        TemporaryData.put(IThirdType.ITemporaryCache.BUY_EXTRA, extraList)
        val list: MutableList<Int> = mutableListOf()
        list.add(IThirdType.IPayType.WX)
        list.add(IThirdType.IPayType.ALI)
        baseNewAdapter.setNewData(list)
        baseNewAdapter.setOnItemClickListener(object :
            BaseNewAdapter.OnItemClickListener<PayHolder, Int> {
            override fun onItemClick(holder: PayHolder?, bean: Int) {
                when (bean) {
                    IThirdType.IPayType.WX -> WXPayUtils.Companion.addOrder(
                        context,
                        payBean.wxAppId,
                        payBean.wxMchId,
                        payBean.wxSerialNo,
                        text!!,
                        price.multiply(BigDecimal(100)).toInt(),
                        object : WxPayListener {
                            override fun onSuccess(result: WxPlayResult) {
                                //调用微信
                                WxUtils(context, lifecycle).wxPay(
                                    result.appId,
                                    result.partnerId,
                                    result.prepayId,
                                    result.nonceStr,
                                    result.timestamp,
                                    result.sign
                                )
                                dismiss()
                            }

                            override fun onFail(msg: String) {
                                postEvent(
                                    payLiveSharedFlow,
                                    PayBus(bean, IThirdType.IPayStatus.FAIL)
                                )
                            }
                        })
                    IThirdType.IPayType.ALI -> AliPayUtils.pay(
                        context as Activity,
                        payBean.aliAppId,
                        payBean.aliRSA2,
                        price.toPlainString(),
                        text,
                        object : PayListener {
                            override fun onSuccess() {
                                postEvent(
                                    payLiveSharedFlow,
                                    PayBus(bean, IThirdType.IPayStatus.SUCCESS)
                                )
                            }

                            override fun onFail() {
                                postEvent(
                                    payLiveSharedFlow,
                                    PayBus(bean, IThirdType.IPayStatus.FAIL)
                                )
                            }
                        })
                }
            }

        })
    }
}